package spitter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import spitter.domain.Spitter;
import spitter.dto.SpitterCondition;
import spitter.repository.SpitterRepository;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/spitter")
public class SpitterController {

    Map<Long, Object> map = new HashMap<>();

//    private final HibernateSpitterRepository repository;
//
//    @Autowired
//    public SpitterController(HibernateSpitterRepository repository) {
//        this.repository = repository;
//    }

//    @RequestMapping(value = "/", method = RequestMethod.GET)
//    public ModelAndView home() {
//        return new ModelAndView("home");
//    }

    @Autowired
    private SpitterRepository repository;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public void spitter(@PathVariable("id") Long id) {
        Spitter spitter = repository.findOne(id);
        System.out.println(spitter);
    }


    @RequestMapping(value = "findByCondition", method = RequestMethod.GET)
    public void spitter(@ModelAttribute SpitterCondition condition) {
        List<Spitter> byCondition = repository.findByCondition(condition);
        System.out.println("__________________________________________");
        byCondition.forEach(System.out::println);
        System.out.println("__________________________________________");
    }


//    @RequestMapping(value = "/register", method = RequestMethod.GET)
//    public String showRegisterForm(Model model) {
////        model.addAttribute(new Spitter());
//
//        return "registerForm";
//    }

//    @RequestMapping("/{spitterId}")
//    public String spitter(Model model, @PathVariable String spitterId) {
//        if (map.containsKey(spitterId)) {
//            model.addAttribute("spitter", map.get(spitterId));
//            return "spitterInfo";
//        }
//        return "redirect:/spitter/register";
//    }

//    @RequestMapping(value = "/register", method = RequestMethod.POST)
//    public String processRegistration(@Valid Spitter spitter, Errors errors,
////                                      @RequestPart("profilePicture") byte[] profilePicture
//                                        MultipartFile file
//    ) throws IOException {
////        if (errors.hasErrors()) {
////            return "registerForm";
////        }
//        //保存到文件系统
//        File dst = new File("D://upload/data/spitter/");
//        if(!dst.exists()) {
//            dst.mkdirs();
//        }
//        File newFile = new File(dst.getPath()+File.separator+file.getOriginalFilename());
//        file.transferTo(newFile);
////        map.put(spitter.getId() + "", spitter);
//        return "redirect:/spitter/" + spitter.getId();
//    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegistration(@Valid Spitter spitter, Errors errors,
//                                      @RequestPart("profilePicture") byte[] profilePicture
                                      MultipartFile profilePicture, RedirectAttributes model) throws IOException {
        if (errors.hasErrors()) {
            return "registerForm";
        }
        //保存到文件系统
        File dst = new File("D://upload/data/spitter/");
        if (!dst.exists()) {
            dst.mkdirs();
        }
        File newFile = new File(dst.getPath() + File.separator + profilePicture.getOriginalFilename());
        profilePicture.transferTo(newFile);
        //把spitter存到map中
        String idStr = (int) (Math.random() * 10000) + "";
        spitter.setId(new Long(idStr));
        map.put(spitter.getId(), spitter);
        model.addAttribute("id", spitter.getId());
        model.addAttribute("username", spitter.getUserName());
        model.addFlashAttribute("spitter", spitter);
        //重定向
        return "redirect:/spitter/{id}";
    }

//    @RequestMapping(value = "save", method = RequestMethod.GET)
//    public String saveSpittle() {
//        //save spittles.....
//        if (true) {
//            throw new DuplicateSpittleException();
//        }
//        return "redirect:/spittles";
//    }

//    @RequestMapping("/{id}")
//    public String spitter(@PathVariable Integer id, Model model) {
//        Spitter spitter = null;
//        if (!model.containsAttribute("spitter")) {
//            spitter = (Spitter) map.get(id);
//            if (spitter == null) {
//                throw new SpitterNotFoundException();
//            }
//        }
//        return "spitterInfo";
//    }


}
